<?php
namespace hbynlsl\spf;

abstract class Controller {
    /**
     * 中间件
     * [
     *     ['class' => 类全名, 'except' => [ 排除的方法名 ]]
     * ]
     */
    protected $middlewares = [];
    // 用户认证通过后的信息
    protected $user = null;


    // 构造方法
    public function __construct() {
        // 获取请求对象
        $request = request();
        // 获取当前路由对象
        $route = $request->getLoadedRoute();
        // 获取当前的请求动作
        if ($route instanceof \Pecee\SimpleRouter\Route\RouteController) {  // 控制器路由对象
            $method = $route->getMethod();
        } else if ($route instanceof \Pecee\SimpleRouter\Route\RouteUrl) { // url形式
            $method = explode('@', $route->getCallback())[1];
        }
        // 处理middlewares
        foreach ($this->middlewares as $middleware) {
            if (!in_array($method, $middleware['except'])) {    // 不在except中
                (new $middleware['class'])->handle($request);
            }
        }
        // 获取认证通过后的user
        $this->user = $request->user;
    }
}
